/**
 * encbuffs.c
 */

#include <xdc/std.h>
#include <ti/sdo/ce/CERuntime.h>
#include <ti/sdo/ce/Engine.h>
#include <ti/sdo/ce/osal/Memory.h>
#include <ti/sdo/ce/video/videnc.h>
#include <ti/sdo/ce/image/imgenc.h>
#include <ti/sdo/ce/trace/gt.h>

#include <Base.h>

#include "avVidMainThrd.h"
#include "avEncBuffs.h"

int getInBuffSize(PicFormat fmt)
{
	int width = getPicWidth(fmt.picfm);
	int height = getPicHeight(fmt.picfm);
	if (width <= 0 || height <= 0)
	{
		LOGERROR("get pic width or height error\n");
		return -1;
	}

	if (fmt.chrome == E_420_TYPE)
	{
		return width * height * 3 / 2;
	}
	else if (fmt.chrome == E_422_TYPE_UYVY)
	{
		return width * height * 2;
	} else
	{
		LOGERROR("chrome = %d is not support\n", fmt.chrome);
		return -1;
	}
}

// NOTE: without in buffer, encode will use mcvip buffer
bool allocEncOutBuff(EncBuffs *pBuffs, int size)
{
	if (pBuffs->pOutBuff == NULL) {
    	memset(pBuffs , 0 ,sizeof(EncBuffs));

    	pBuffs->outBuffSize = size;
    	pBuffs->pOutBuff = (char *) Memory_contigAlloc(pBuffs->outBuffSize, Memory_DEFAULTALIGNMENT);
    	pBuffs->pDenoiseBuff = (char *) Memory_contigAlloc(704 * 576 * 3/2, Memory_DEFAULTALIGNMENT);
    	if ( pBuffs->pOutBuff == NULL && pBuffs->pDenoiseBuff == NULL )
    	{
    		LOGERROR("pBuffs->pOutBuff == NULL\n");
    		return false;
    	}
    }
    memset(pBuffs->pOutBuff, 0, pBuffs->outBuffSize);
    memset(pBuffs->pDenoiseBuff, 0, pBuffs->inFrameSize);
    return true;
}

void freeEncOutBuff(EncBuffs *pBuffs)
{
	if (pBuffs->pOutBuff != NULL)
	{
		Memory_contigFree(pBuffs->pOutBuff, pBuffs->outBuffSize);
		pBuffs->pOutBuff = NULL;
	}
}

